package com.yf.tz.controller.api;

import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import com.yf.response.RRException;
import com.yf.tz.entity.MallAddressEntity;
import com.yf.tz.service.MallAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/address")
public class ApiAddressController {

    @Autowired
    private MallAddressService  mallAddressService;


    //新增或者修改商城地址
    @PostMapping("/mall")
    public R mallAddress(@LoginUserId Long userId,MallAddressEntity addressEntity){
        mallAddressService.merge(userId,addressEntity);
        return R.ok(addressEntity);
    }

    //获取商城所有地址
    @GetMapping("/mall")
    public R mallAddress(@LoginUserId Long userId){
        List<MallAddressEntity> addressEntities =  mallAddressService.findByUserId(userId);
        return R.ok(addressEntities);
    }

    //获取商城地址详情
    @GetMapping("/mall/info")
    public R mallAddressInfo(@LoginUserId Long userId,Long id){
        MallAddressEntity mallAddressEntity = mallAddressService.get(id);
        if(!mallAddressEntity.getUserId().equals(userId)){
            throw new RRException("数据异常");
        }
        return R.ok(mallAddressEntity);
    }


    //删除商城地址
    @PostMapping("/mall/delete")
    public R deleteMallAddress(@LoginUserId Long userId, Long id){
        MallAddressEntity mallAddressEntity = mallAddressService.get(id);
        if(!mallAddressEntity.getUserId().equals(userId)){
            throw new RRException("数据异常");
        }
        mallAddressService.delete(id);
        return R.ok();
    }






}
